﻿/// <reference path="Event.js" />
/// <reference path="../jquery.jqGrid-4.4.4/js/jquery.jqGrid.src.js" />
/// <reference path="../jquery-1.9.1.js" />


var ClientTrigger = {
    controls: {
        clientTriggerGrid: $("#c-event-list")
    },
    SignalRURL: "Check",
    init: function () {
        var clientTriggerGrid = ClientTrigger.controls.clientTriggerGrid

        var jqGridDummyData1 = [
            { title: 'X-Ray', room: 'Alonzo',personincharge: 'Dr. Frederick Magalang' },
            { title: 'Blood Chem', room: 'Rizal', personincharge: 'Dr. Jerome Corpuz' },
            { title: 'Physical Exam', room: 'Ponder Haven 1', personincharge: 'Dr. Jeffrey Edubas' },
            { title: 'Eye Exam', room: 'Burgos', personincharge: 'Dr. Frederico Tumon' }
        ]

        //clientTriggerGrid.jqGrid({
        //    //datatype: "local",
        //    jsonReader: { repeatitems: false },
        //    dataType: "json",
        //    url: "/queue/GetUserTriggeredEvents?userId=3",
        //    height: 250,
        //    width: 360,
        //    colModel: [
        //        { name: 'Name', index: 'Name', label: 'Event Name' },
        //        { name: 'Room', index: 'Room', label: 'Room' },
        //        {
        //            name: 'action', index: 'action', label: ' ', fixed: true, width: 70, sortable: false,
        //            formatter: function (cellvalue, options, rowObjec) {
        //                var obj = JSON.stringify(rowObjec);
        //                return '<span class="fui-settings-24" style="font-size:35px;padding-left:15px;cursor:pointer" onclick="ClientTrigger.ControlEvent(' + options.rowId + ', this);"></span>';
        //            }
        //        },
        //        { name: 'PersonInCharge', index: 'PersonInCharge', label: '', hidden: true },
        //    ],
        //    loadComplete: function() {
        //        var grid = $("#c-event-list");
        //        var ids = grid.getDataIDs();

                

        //        for (var i = 0; i < ids.length; i++) {
        //            grid.setRowData ( ids[i], false, {height: 20+i*2} );
        //        }
        //        // grid.setGridHeight('auto');
        //    }
        //});

        clientTriggerGrid.jqGrid({
            url: "/queue/GetUserTriggeredEvents?userId=1",
            datatype: "json",
            jsonReader: { repeatitems: false },
            height: 250,
            width: 360,
            colModel: [
                { name: 'Name', index: 'Name', label: 'Event Name' },
                { name: 'Room', index: 'Room', label: 'Room' },
                {
                    name: 'action', index: 'action', label: ' ', fixed: true, width: 70, sortable: false,
                    formatter: function (cellvalue, options, rowObjec) {
                        var obj = JSON.stringify(rowObjec);
                        return '<span class="fui-settings-24" style="font-size:35px;padding-left:15px;cursor:pointer" onclick="ClientTrigger.ControlEvent(' + options.rowId + ', this);"></span>';
                    }
                },
                { name: 'PersonInCharge', index: 'PersonInCharge', label: '', hidden: true },
            ],
            loadComplete: function () {
                var grid = $("#list");
                var ids = grid.getDataIDs();

                for (var i = 0; i < ids.length; i++) {
                    grid.setRowData(ids[i], false, { height: 20 + i * 2 });
                }
                // grid.setGridHeight('auto');
            }
        });

        //for (var i = 0; i <= jqGridDummyData1.length; i++)
        //    clientTriggerGrid.jqGrid('addRowData', i + 1, jqGridDummyData1[i]);

        var body = document.body,
            html = document.documentElement;

        var height = Math.max(body.scrollHeight, body.offsetHeight,
                               html.clientHeight, html.scrollHeight, html.offsetHeight);

        clientTriggerGrid.jqGrid("setGridHeight", height - 415);
        $("#c-subs-event-main-container").css("height", height - 115);

        ClientTrigger.Connection = $.connection(ClientTrigger.SignalRURL);

        ClientTrigger.Connection.received(function (data) {
            ClientTrigger.DecodeMessage(data);
        });

        ClientTrigger.Connection.start();
        ClientTrigger.initEvents();
    },
    ControlEvent: function (d, e) {
        if ($("#c-div-event-container").find(".unliq-event").length < 4) {
            if ($(e).hasClass('fui-settings-24')) {
                var rowData = ClientTrigger.controls.clientTriggerGrid.jqGrid('getRowData', d);
                $(e).removeClass('fui-settings-24').addClass("fui-man-24");

                var prefix = rowData.Name.replace(/ /g, '').toLowerCase();
                $("#c-div-event-container").append("<div id='c-div-" + prefix + "'></div>");
                $('#c-div-' + prefix).cQevent({
                    title: rowData.Name,
                    room: rowData.Room,
                    personincharge: rowData.PersonInCharge,
                    closeFn: function () {
                        $(e).removeClass('fui-man-24').addClass("fui-settings-24");
                    }
                });

            }
        }
    },

    initEvents: function () {

        $(document).on("click", ".btn-info", function () {

            var eventName = $(this).attr('eventname');

            ClientTrigger.Connection = $.connection(ClientTrigger.SignalRURL);

            ClientTrigger.Connection.start().done(function () {
                var result = ClientTrigger.Connection.send("UPDATE_NOWSERVING:" + eventName);
            });

            //ClientTrigger.Connection.send("UPDATE_NOWSERVING:" + eventName);

        });

    },

    Reserve: function () {

    },
    DecodeMessage: function (data) {

        if (data && data.Type) {

            var model = data.Model;
            switch (data.Type) {

                case "UPDATE_NOWSERVING":
                    {
                        $("#c-" + data.Model + "nowserving-num").html(data.Message);
                        break;
                    }
            }
            return;
        }
    }

}

$(document).ready(function () {
    ClientTrigger.init();
})
